home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / SpellCompositor / SpellRegisterVector.java < prev    next >
Encoding:
Java Source  |  2001-06-23  |  6.0 KB  |  195 lines

  1. /*
  2. This file and its intellectual contents are considered property of the creator and are to be treated as such with respect to use in other applications.  Any use of this software for any purpose whatsoever must have explicit permission from the author of the file.  This code is part of an ongoing development process for future possible products, and so is considered private property.
  3. Do not use without permission.  Author: Scott C. Ziegler <Aslan@Narnia.net>
  4. */
  5.  
  6. package Arcana;
  7.  
  8. import java.awt.*;
  9. import java.io.*;
  10. import java.util.*;
  11. import java.awt.event.*;
  12. import java.awt.Color;
  13.  
  14. // SpellRegisterVector
  15. //
  16. //        Primary data-structure for the storage and manipulation of 
  17. //        SpellRegisterRecord entries
  18.     
  19. public class SpellRegisterVector implements Serializable {
  20.  
  21.     static final int BY_SCHOOL = 0;
  22.     static final int BY_LEVEL = 1;
  23.     static final int BY_COMPONENTS = 2;
  24.  
  25.     Vector SpellRegisterVec;
  26.     
  27.     // Constructors
  28.     
  29.     public SpellRegisterVector() {
  30.         SpellRegisterVec = new Vector(50);
  31.         }
  32.     
  33.     public SpellRegisterVector(SpellRecord[] spellArray) {
  34.         SpellRegisterVec = new Vector(spellArray.length);
  35.         
  36.         for (int i=0; i < spellArray.length; i++) {    
  37.             SpellRegisterVec.addElement(spellArray[i].getSpellRegister());
  38.             }
  39.         }
  40.     
  41.     // the Vector parameter MUST BE of SpellRegisters or the program will malfunction
  42.     public SpellRegisterVector(Vector initSpellVector) {
  43.         SpellRegisterVec = new Vector(initSpellVector.size());
  44.         setSpellRegisterVector(initSpellVector);
  45.         }
  46.     
  47.     // Selectors
  48.     
  49.     public Vector getSpellRegisterVector() {
  50.         return SpellRegisterVec;
  51.         }
  52.     
  53.     public SpellRegister getSpellRegister(int index) {
  54.         return (SpellRegister)SpellRegisterVec.elementAt(index);
  55.         }
  56.         
  57.     public Vector getSpellNamesVector() {
  58.         Vector vec = new Vector(SpellRegisterVec.size());
  59.         Enumeration e = SpellRegisterVec.elements();
  60.         SpellRegister sentinel; // holds SpellRegister to 
  61.         while (e.hasMoreElements()) {
  62.             sentinel = (SpellRegister)e.nextElement();
  63.             vec.addElement(sentinel.getName());
  64.             }
  65.         return vec;
  66.         }
  67.     
  68.     // Mutators
  69.  
  70.     // function - take spellvector as an argument and reassign the SpellRegister with it
  71.     
  72.     public void setSpellRegisterVector(Vector spellvector) {
  73.         java.util.Enumeration e = spellvector.elements();
  74.         
  75.         if (SpellRegisterVec.size() > 0) {
  76.             SpellRegisterVec.removeAllElements();
  77.             }
  78.         SpellRegister Sentinel;
  79.         while (e.hasMoreElements()) {
  80.             // this line makes sure that non-SpellRegisters don't get through
  81.             Sentinel = (SpellRegister)e.nextElement(); 
  82.             // as Sentinel can only be of type SpellRegister.
  83.             SpellRegisterVec.addElement(Sentinel);
  84.             }
  85.         }
  86.         
  87.     // function - take spellvector as an argument and append it to the SpellRegister
  88.         
  89.     public void appendSpellVector(Vector spellvector) {
  90.         java.util.Enumeration e = spellvector.elements();
  91.         
  92.         SpellRegister Sentinel;
  93.         while (e.hasMoreElements()) {
  94.             Sentinel = (SpellRegister)e.nextElement(); 
  95.             SpellRegisterVec.addElement(Sentinel);
  96.             }
  97.         }
  98.         
  99.     // functions - adds spell(s) to vector in alphabetical order (by reordering Vector)
  100.     
  101.     public void addSpell(SpellRecord spell) {
  102.         SpellRegisterVec.addElement(spell.getSpellRegister());
  103.         SpellRegister[] sortingArray = new SpellRegister[SpellRegisterVec.size()];
  104.         SpellRegisterVec.copyInto(sortingArray);
  105.         Sorter.sort(sortingArray);
  106.         // SpellRegisterVec = new Vector(sortingArray);
  107.         this.setSpellRegisterVector(this.arrayToVector(sortingArray));
  108.         }
  109.         
  110.     public void addSpells(SpellRecord[] spell) {
  111.         SpellRegister[] sortingArray = 
  112.                             new SpellRegister[SpellRegisterVec.size() + spell.length];
  113.         // this loop takes the news spells and inserts them into the new
  114.         // spaces on the end of the sortingArray.
  115.         for (int i = SpellRegisterVec.size(), j = 0; 
  116.              i < (spell.length + SpellRegisterVec.size()); 
  117.              i++, j++) {
  118.             sortingArray[i] = spell[j].getSpellRegister();
  119.             }
  120.         Sorter.sort(sortingArray);
  121.         this.setSpellRegisterVector(this.arrayToVector(sortingArray));
  122.         }
  123.     
  124.     public void removeSpell(SpellRecord spell) {    
  125.         if (!SpellRegisterVec.removeElement(spell.getSpellRegister())) {    
  126.             // JOptionPane ... ERROR!
  127.             }
  128.         this.sortSpells();
  129.         }
  130.     
  131.     
  132.     
  133.     // Methods
  134.     
  135.     public Vector arrayToVector(Object[] objArray) {
  136.         Vector newVec = new Vector(objArray.length);
  137.         
  138.         for (int i = 0; i < objArray.length; i++) {
  139.             newVec.addElement(objArray[i]);
  140.             }
  141.         return newVec;
  142.         }
  143.         
  144.     
  145.     // function - reorders the vector in alphabetical order according to name
  146.     
  147.     public void sortSpells() {
  148.         SpellRegister[] sortingArray = new SpellRegister[SpellRegisterVec.size()];
  149.         SpellRegisterVec.copyInto(sortingArray);
  150.         Sorter.sort(sortingArray);
  151.         this.setSpellRegisterVector(this.arrayToVector(sortingArray));
  152.         }
  153.         
  154.     
  155.     // function - reorders the vector by criteria (school/level/Component)
  156.     
  157.     public void sortByField(int field) {
  158.         SpellRegister[] sortingArray = new SpellRegister[SpellRegisterVec.size()];
  159.         SpellRegisterVec.copyInto(sortingArray);
  160.         
  161.         switch (field) {
  162.             case BY_SCHOOL :
  163.                 Sorter.sort(sortingArray, new Sorter.Comparer() {
  164.                     public int compare(Object a, Object b) {
  165.                         SpellRegister adin = (SpellRegister)a, dva = (SpellRegister)b;
  166.                         if ((adin.getSchool()).compareTo(dva.getSchool()) == 0) 
  167.                             return (adin.getName()).compareTo(dva.getName());
  168.                         else return (adin.getSchool()).compareTo(dva.getSchool());
  169.                         }
  170.                     });
  171.                 break;
  172.             case BY_LEVEL :
  173.                 Sorter.sort(sortingArray, new Sorter.Comparer() {
  174.                     public int compare(Object a, Object b) {
  175.                         SpellRegister adin = (SpellRegister)a, dva = (SpellRegister)b;
  176.                         if (adin.getLevel() == dva.getLevel()) 
  177.                             return (adin.getName()).compareTo(dva.getName());
  178.                         else return (adin.getLevel() - dva.getLevel());
  179.                         }
  180.                     });
  181.                 break;
  182.             case BY_COMPONENTS :
  183.                 Sorter.sort(sortingArray, new Sorter.Comparer() {
  184.                     public int compare(Object a, Object b) {
  185.                         SpellRegister adin = (SpellRegister)a, dva = (SpellRegister)b;
  186.                         if (adin.getComponents() == dva.getComponents()) 
  187.                             return (adin.getName()).compareTo(dva.getName());
  188.                         else return (adin.getComponents() - dva.getComponents());
  189.                         }
  190.                     });
  191.                 break;
  192.                 }
  193.         }
  194.     
  195.     }